<?php
	require("libraries/student.php");
	class Ads extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->helper(array("form"));
			$this->load->library("form_validation");
			$this->load->model("model_ads");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/ads/index/';
			$config['total_rows'] = $this->model_ads->count_all();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title'] = "Quản lý quảng cáo";
			$data['template'] = "advertise/list";
			$data['data']['info']= $this->model_ads->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['title'] = "Thêm mới quảng cáo";
			$data['template'] = "advertise/add";
			$data['data'] = "";
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("ads_title","Tiêu đề quảng cáo","min_length[5]|required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('ads_title'));
					  $url = str_replace(' ','-',strtolower($this->string->replace($title)));
					  $db = array(
							"ads_title" => $this->input->post("ads_title"),
							"ads_order" => $this->input->post("ads_order"),
							"ads_info" => $this->input->post("ads_info"),
							"ads_full"  => $this->input->post("ads_full"),	
							"ads_position"	=> $this->input->post("ads_position"),			
						);
					$this->model_ads->add($db);
				    redirect(base_url()."admin/ads");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$data['title'] = "Sửa quảng cáo";
			$data['template'] = "advertise/edit";
			$id = $this->uri->segment(4);
			$data['data']['result'] = $this->model_ads->getdata($id);
			if($data['data']['result'] == NULL){redirect(base_url());}
			$data['stt'] = $data['data']['result']['ads_status'];
			//die($data['stt'] );
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("ads_title","Tiêu đề quảng cáo","min_length[5]|required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('ads_title'));
					  $url = str_replace(' ','-',strtolower($this->string->replace($title)));
					  $db = array(
							"ads_title" => $this->input->post("ads_title"),
							"ads_order" => $this->input->post("ads_order"),
							"ads_info" => $this->input->post("ads_info"),
							"ads_full"  => $this->input->post("ads_full"),	
							"ads_status"  => $this->input->post("ads_status"),	
							"ads_position"	=> $this->input->post("ads_position"),			
						);
					$this->model_ads->update($db,$id);
				    redirect(base_url()."admin/ads");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			//$data = $this->model_ads->getdata($id);
			$this->model_ads->del($id);
			redirect(base_url()."admin/ads");
		}
		public function createThumbnail($parth,$fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = $parth.'/'.$fileName;
			$config['new_image'] = $parth.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}